package sy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.BaseDaoI;
import sy.model.TsupplierInfo;
import sy.model.TsupplierOffer;
import sy.model.TvehicleInfo;
import sy.model.TvehicleToffer;
import sy.pageModel.DataGrid;
import sy.pageModel.SupplierInfo;
import sy.pageModel.VehicleInfo;
import sy.service.VehicleInfoServiceI;

@Service("vehicleInfoService")
public class VehicleInfoServiceImpl implements VehicleInfoServiceI {

	private static final Logger logger = Logger.getLogger(VehicleInfoServiceImpl.class);

	private BaseDaoI<TvehicleInfo> vehicleInfoDao;
	private BaseDaoI<TsupplierOffer> offerDao;
	private BaseDaoI<TvehicleToffer> vehicleOfferDao;

	public BaseDaoI<TvehicleInfo> getVehicleInfoDao() {
		return vehicleInfoDao;
	}

	@Autowired
	public void setVehicleInfoDao(BaseDaoI<TvehicleInfo> vehicleInfoDao) {
		this.vehicleInfoDao = vehicleInfoDao;
	}

	
	public BaseDaoI<TsupplierOffer> getOfferDao()
    {
        return offerDao;
    }
	
	@Autowired
    public void setOfferDao(BaseDaoI<TsupplierOffer> offerDao)
    {
       this.offerDao = offerDao;
    }

    public BaseDaoI<TvehicleToffer> getVehicleOfferDao()
    {
        return vehicleOfferDao;
    }
    
    @Autowired
    public void setVehicleOfferDao(BaseDaoI<TvehicleToffer> vehicleOfferDao)
    {
        this.vehicleOfferDao = vehicleOfferDao;
    }

    public VehicleInfo save(VehicleInfo vehicleInfo) {
		TvehicleInfo t = new TvehicleInfo();
		BeanUtils.copyProperties(vehicleInfo, t);
		t.setVehicleid(UUID.randomUUID().toString());
		vehicleInfoDao.save(t);
		if (vehicleInfo.getOfferIds() != null) {
			String offerIds = "";
			String prices = "";
			for (String id : vehicleInfo.getOfferIds().split(",")) {
				TsupplierOffer r = offerDao.get(TsupplierOffer.class, id);
				if (r != null) {
				    System.out.println("dslflksdlk");
					TvehicleToffer vo = new TvehicleToffer();
					vo.setId(UUID.randomUUID().toString());
					vo.setToffer(r);
					vo.setTvehicle(t);
					vehicleOfferDao.save(vo);

					if (offerIds.length() > 0) {
					    offerIds += ",";
					    prices += ",";
					}
					offerIds += r.getOfferid();
					prices += r.getPrice();
				}
			}
			vehicleInfo.setOfferIds(offerIds);
			vehicleInfo.setPrices(prices);
		}
		BeanUtils.copyProperties(t, vehicleInfo);
		return vehicleInfo;
	}

	
	public VehicleInfo find(VehicleInfo vehicleInfo) {
	/*	Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", vehicleInfo.getName());
		params.put("pwd", Encrypt.e(vehicleInfo.getPwd()));
		TvehicleInfo t = vehicleInfoDao.get("from TvehicleInfo t where t.name=:name and t.pwd=:pwd", params);
		if (t != null) {
			BeanUtils.copyProperties(t, vehicleInfo);

			String roleIds = "";
			String roleNames = "";
			String resourceIds = "";
			String resourceNames = "";
			List<String> resourceUrls = new ArrayList<String>();
			Set<TvehicleInfoTrole> vo = t.getTvehicleInfoTroles();// 获得当前用户的所有角色
			if (vo != null && !vo.isEmpty()) {
				for (TvehicleInfoTrole tur : vo) {
					if (roleIds.length() > 0) {
						roleIds += ",";
						roleNames += ",";
					}
					roleIds += tur.getTrole().getId();
					roleNames += tur.getTrole().getText();

					Set<TroleTresource> rr = tur.getTrole().getTroleTresources();// 获得当前角色所有资源
					if (rr != null && !rr.isEmpty()) {
						for (TroleTresource trr : rr) {
							if (resourceIds.length() > 0) {
								resourceIds += ",";
								resourceNames += ",";
							}
							resourceIds += trr.getTresource().getId();
							resourceNames += trr.getTresource().getText();
							resourceUrls.add(trr.getTresource().getUrl());
						}
					}
				}
			}
			vehicleInfo.setRoleIds(roleIds);
			vehicleInfo.setRoleNames(roleNames);
			vehicleInfo.setResourceIds(resourceIds);
			vehicleInfo.setResourceNames(resourceNames);
			vehicleInfo.setResourceUrls(resourceUrls);

			return vehicleInfo;
		}*/
		return null;
	}

	
	public DataGrid datagrid(VehicleInfo vehicleInfo) {
		DataGrid dg = new DataGrid();
		String hql = "from TvehicleInfo t ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(vehicleInfo, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(vehicleInfo, hql);
		List<TvehicleInfo> l = vehicleInfoDao.find(hql, params, vehicleInfo.getPage(), vehicleInfo.getRows());
		List<VehicleInfo> nl = new ArrayList<VehicleInfo>();
		changeModel(l, nl);
		dg.setTotal(vehicleInfoDao.count(totalHql, params));
		dg.setRows(nl);
		if(nl.size()>0){
		}
		return dg;
	}

	private void changeModel(List<TvehicleInfo> l, List<VehicleInfo> nl) {
		if (l != null && l.size() > 0) {
			for (TvehicleInfo t : l) {
				VehicleInfo vehicleInfo = new VehicleInfo();
				BeanUtils.copyProperties(t, vehicleInfo);

				Set<TvehicleToffer> vo = t.getTvehicleToffers();
				if (vo != null && !vo.isEmpty()) {
					String offerIds = "";
					String prices = "";

					for (TvehicleToffer tt : vo) {
						if (offerIds.length() > 0) {
						    offerIds += ",";
							prices += ",";
						}
						offerIds += tt.getToffer().getOfferid();
						prices += tt.getToffer().getPrice();
					}

					vehicleInfo.setOfferIds(prices);
					vehicleInfo.setPrices(prices);
				}

				nl.add(vehicleInfo);
			}
		}
	}

	private String addOrder(VehicleInfo vehicleInfo, String hql) {
		if (vehicleInfo.getSort() != null) {
			hql += " order by " + vehicleInfo.getSort() + " " + vehicleInfo.getOrder();
		}
		return hql;
	}

	private String addWhere(VehicleInfo vehicleInfo, String hql, Map<String, Object> params) {
		hql += " where 1=1 ";
		/*if (vehicleInfo.getCreatedatetimeStart() != null) {
			hql += " and t.createdatetime >= :getCreatedatetimeStart";
			params.put("getCreatedatetimeStart", vehicleInfo.getCreatedatetimeStart());
		}
		if (vehicleInfo.getCreatedatetimeEnd() != null) {
			hql += " and t.createdatetime <= :getCreatedatetimeEnd";
			params.put("getCreatedatetimeEnd", vehicleInfo.getCreatedatetimeEnd());
		}
		if (vehicleInfo.getModifydatetimeStart() != null) {
			hql += " and t.modifydatetime >= :getModifydatetimeStart";
			params.put("getModifydatetimeStart", vehicleInfo.getModifydatetimeStart());
		}
		if (vehicleInfo.getModifydatetimeEnd() != null) {
			hql += " and t.modifydatetime <= :getModifydatetimeEnd";
			params.put("getModifydatetimeEnd", vehicleInfo.getModifydatetimeEnd());
		}
		if (vehicleInfo.getQ() != null && !vehicleInfo.getQ().trim().equals("")) {
			hql += " and t.name like :name ";
			params.put("name", "%%" + vehicleInfo.getQ().trim() + "%%");
		}
		if (vehicleInfo.getName() != null && !vehicleInfo.getName().trim().equals("")) {
			hql += " and t.name like :name";
			params.put("name", "%%" + vehicleInfo.getName().trim() + "%%");
		}*/
		return hql;
	}

	
	public List<VehicleInfo> combobox(VehicleInfo vehicleInfo) {
		String hql = "from TvehicleInfo t ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(vehicleInfo, hql, params);
		hql += " order by name";
		List<TvehicleInfo> l = vehicleInfoDao.find(hql, params, 1, 10);
		List<VehicleInfo> nl = new ArrayList<VehicleInfo>();
		changeModel(l, nl);
		return nl;
	}

	
	public void remove(String ids) {
		for (String id : ids.split(",")) {
			TvehicleInfo t = vehicleInfoDao.get(TvehicleInfo.class, id);
			if (t != null) {
				vehicleInfoDao.delete(t);
			}
		}
	}

	
	public VehicleInfo edit(VehicleInfo vehicleInfo) {
		TvehicleInfo t = vehicleInfoDao.get(TvehicleInfo.class, vehicleInfo.getVehicleid());
		if (t != null) {
			
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("tvehicle", t);
			vehicleOfferDao.executeHql("delete TvehicleToffer t where t.tvehicle=:tvehicle", params);
			vehicleInfoDao.saveOrUpdate(t);
			BeanUtils.copyProperties(vehicleInfo, t);

			if (vehicleInfo.getOfferIds() != null) {
				String offerIds = "";
				String prices = "";
				for (String id : vehicleInfo.getOfferIds().split(",")) {
					TsupplierOffer offer = offerDao.get(TsupplierOffer.class, id);
					if (offer != null) {
					   TvehicleToffer vo = new TvehicleToffer();
						vo.setId(UUID.randomUUID().toString());
						vo.setToffer(offer);
						vo.setTvehicle(t);
						vehicleOfferDao.save(vo);

						if (offerIds.length() > 0) {
						    offerIds += ",";
						    prices += ",";
						}
						offerIds += offer.getOfferid();
						prices += offer.getPrice();
					}
				}
				vehicleInfo.setOfferIds(offerIds);
				vehicleInfo.setPrices(prices);
			}
			System.out.println(vehicleInfo.getVehicletype());
			return vehicleInfo;
		}
		return vehicleInfo;
	}

	
	public void modifyRole(VehicleInfo vehicleInfo) {
		/*for (String uid : vehicleInfo.getIds().split(",")) {
			TvehicleInfo u = vehicleInfoDao.get(TvehicleInfo.class, uid);
			if (u != null) {
				u.setModifydatetime(new Date());
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("tvehicleInfo", u);
				vehicleInfoRoleDao.executeHql("delete TvehicleInfoTrole t where t.tvehicleInfo=:tvehicleInfo", params);
				if (vehicleInfo.getRoleIds() != null) {
					for (String id : vehicleInfo.getRoleIds().split(",")) {
						TsupplierOffer r = roleDao.get(TsupplierOffer.class, id);
						if (r != null) {
							TvehicleInfoTrole vo = new TvehicleInfoTrole();
							vo.setId(UUID.randomUUID().toString());
							vo.setTrole(r);
							vo.setTvehicleInfo(u);
							vehicleInfoRoleDao.save(vo);
						}
					}
				}
			}
		}*/
	}

	
	public void modifyPwd(VehicleInfo vehicleInfo) {
		/*TvehicleInfo u = vehicleInfoDao.get(TvehicleInfo.class, vehicleInfo.getId());
		if (u != null) {
			u.setPwd(Encrypt.e(vehicleInfo.getPwd()));
		}*/
	}
}
